<?php
/**
 * Nordic Repository
 * 
 * LICENSE
 * 
 * The new BSD license is applied on this source-file. For further
 * information please visit http://license.nordic-dev.de/newbsd.txt
 * or send an email to andre.moelle@gmail.com.
 */

/**
 * Necessary includes.
 */
Zend_Loader::loadClass('Zend_Validate_Interface');
Zend_Loader::loadClass('Zend_Validate_Exception');
Zend_Loader::loadClass('Nordic_Validate_Chain_Element');

/**
 * Validate-chain.
 * 
 * @category   Nordic
 * @package    Nordic_Validate
 * @copyright  2007 Nordic Development
 * @license    http://license.nordic-dev.de/newbsd.txt (New-BSD license)
 * @author     Andre Moelle <andre.moelle@gmail.com>
 * @version    $Id: Chain.php 3 2007-07-08 09:21:42Z andre.moelle $
 */
class Nordic_Validate_Chain implements Zend_Validate_Interface
{
	/**
	 * Chain which contains the "Nordic_Validate_Chain_Element"
	 * objects.
	 * 
	 * @var array
	 */
	private $_chain = array();
	
	/**
	 * Array containing the error-messages.
	 * 
	 * @var array
	 */
	private $_messages = array();
	
	/**
	 * Constructor.
	 * 
	 * You can assign one or more "Nordic_Validate_Chain_Element"
	 * objects. These are added to the internal chain.
	 * 
	 * @param Nordic_Validate_Chain_Element $element chain-element
	 * @return void
	 */
	public function __construct (Nordic_Validate_Chain_Element $element = null)
	{
		if(func_num_args())
		{
			call_user_func_array(array($this, 'append'), func_get_args());
		}
	}
	
	/**
	 * Appends an element to the chain.
	 * 
	 * @param Nordic_Validate_Chain_Element $element chain-element
	 * @return void
	 */
	public function append (Nordic_Validate_Chain_Element $element)
	{
		foreach(func_get_args() as $element)
		{
			if(!($element instanceof Nordic_Validate_Chain_Element))
			{
				continue;
			}
			
			$this->_chain[] = $element;
		}
	}
	
	/**
	 * @see Zend_Validate_Interface::getErrors()
	 */
	public function getErrors ()
	{
		return array();
	}
	
	/**
	 * @see Zend_Validate_Interface::getMessages()
	 */
	public function getMessages ()
	{
		return $this->_messages;
	}
	
	/**
	 * @see Zend_Validate_Interface::isValid()
	 */
	public function isValid ($value)
	{
		$this->_messages = array();
		
		foreach($this->_chain as $element)
		{
			if($element->isValid(null))
			{
				continue;
			}
			
			$msg = $element->getMessages();
			$this->_messages[] = array_shift($msg);
		}
		
		return !count($this->_messages);
	}
}
?>